import javax.swing.text.html.ListView;
import java.time.chrono.IsoChronology;
import java.util.Stack;

import static jdk.nashorn.internal.objects.NativeString.charAt;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-07-08
 * Time: 10:01
 */
public class MyStackTest {

    public static void main(String[] args) {
        int[] pushV = {1,2,3,4,5};
        int[] popV = {4,5,3,1,2};
        System.out.println(IsPopOrder(pushV,popV));
    }
    //栈的压入 弹出序列
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (j < popV.length && !stack.empty() && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        if (j == popV.length) {
            return true;
        }
        return false;
    }


    public static void main3(String[] args) {
        String str = "(])";
        System.out.println(isValid(str));
    }
    //括号匹配
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char chLeft = s.charAt(i);
            if (chLeft == '(' || chLeft == '{' || chLeft == '[') {
                stack.push(chLeft);
            } else {
                //都是右括号
                if (stack.empty()) {
                    return false;
                }
                char chRight = s.charAt(i); //右括号
                //判断左右括号匹配,匹配弹出,不匹配直接false
                if (stack.peek() == '(' && chRight == ')' || stack.peek() == '{' && chRight == '}' || stack.peek() == '[' && chRight == ']') {
                    stack.pop();
                }else {
                    return false;
                }
            }

        }
        //都是左括号
        if (!stack.empty()) {
            return false;
        }
        return true;
    }

    public static void main2(String[] args) {
        String[] tokens = {"2","1","+","3","*"};
        int a = evalRPN(tokens);
        System.out.println(a);
    }
    //逆波兰表达式求值
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            //如果是数字的话就压栈
            if (tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*") || tokens[i].equals("/")) {
                int rightNum = stack.pop();
                int leftNum = stack.pop();
                switch (tokens[i]) {
                    case "+":
                        stack.push(leftNum + rightNum);
                        break;
                    case "-":
                        stack.push(leftNum - rightNum);
                        break;
                    case "*":
                        stack.push(leftNum * rightNum);
                        break;
                    case "/":
                        stack.push(leftNum / rightNum);
                        break;
                }
            } else {
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.pop();
    }




    public static void main1(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        myStack.push(5);
        myStack.push(6);

        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.peek());

        System.out.println(myStack.empty());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());

        System.out.println(myStack.size());
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        System.out.println(myStack.size());

    }
}
